import urllib.request
import json
from sqlalchemy import and_
from db import Asset, HistoryPrice, ExchangePlace
import db

class YahooProvider:
	""" 
		Handle the different data sets for assets ticker lookup
		and historical data lookup 
	"""
	
	def lookup_ticker(asset_name):
		"""
			Help parsing json response from yahoo service :
			the response is formatted as follow 
			YAHOO.Finance.SymbolSuggest.ssCallback({"ResultSet":{
				"Query":"yahoo",
				"Result":[{
				"symbol":"YHOO","name": "Yahoo! Inc.","exch": "NMS","type": "S","exchDisp":"NASDAQ"},{
				"symbol":"YHO.HM","name": "YAHOO","exch": "HAM","type": "S","exchDisp":"Hamburg"},{
				"symbol":"YHO.BE","name": "YAHOO","exch": "BER","type": "S","exchDisp":"Berlin"},{
				"symbol":"ETD","name": "Citigroup Inc. ELKS On Yahoo","exch": "PCX","type": "S"},{
				"symbol":"YOJ.BE","name": "YAHOO JAPAN","exch": "BER","type": "S","exchDisp":"Berlin"},{
				"symbol":"YHOO.MX","name": "YAHOO","exch": "MEX","type": "S","exchDisp":"Mexico"},{
				"symbol":"YHO.SG","name": "YAHOO","exch": "STU","type": "S","exchDisp":"Stuttgart"},{
				"symbol":"YAHOF.PK","name": "YAHOO JAPAN CORP","exch": "PNK","type": "S","exchDisp":"Pink Sheets"},{
				"symbol":"YOJ.DE","name": "YAHOO JAPAN","exch": "GER","type": "S","exchDisp":"XETRA"},{
				"symbol":"YHO.DU","name": "YAHOO","exch": "DUS","type": "S"}]}})
		"""
		url = "http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=%s&callback=YAHOO.Finance.SymbolSuggest.ssCallback" % (asset_name)
		yahoo_called = urllib.request.urlopen(url)
		json_response = str(yahoo_called.read()).replace("b'YAHOO.Finance.SymbolSuggest.ssCallback(", "").replace(")'","")
		response_obj = json.loads(json_response)
		#print ("Got response : " + json.dumps(response_obj, sort_keys=True, indent=4))
		results = response_obj.get("ResultSet").get("Result")
		YahooProviderDataHandler.extract_assets(results)
		
	def history_extractor(session, ticker):
		"""
			Calls the yahoo financial service to get historical data
			http://ichart.finance.yahoo.com/table.csv?s=YHOO&a=03&b=12&c=1996&d=02&e=29&f=2010&g=d&ignore=.csv
		"""
		# open file for test
		url = "http://ichart.finance.yahoo.com/table.csv?s=%s&a=03&b=12&c=2004&d=02&e=29&f=2011&g=d&ignore=.csv" % (ticker)
		try:
			yahoo_called = urllib.request.urlopen(url)
		except:
			print ("Connection refused")
			return;
			
		ref_asset = session.query(Asset).filter(Asset.name == "Yahoo").one()
		prices = []
		while yahoo_called.readable():
			line = str(yahoo_called.readline())[2:-1]
			if line:
				#print (line.replace("\n",""))
				price = YahooProviderDataHandler.extract_history_prices(ref_asset, line.split(","))
				if price and session.query(HistoryPrice).filter(and_(HistoryPrice.asset == ref_asset,
						HistoryPrice.price_date == price.price_date )).count() == 0: #check map
					print ("Adding record for asset %s at date %s " % (price.asset, price.price_date))
					prices.append(price)
				elif price:
					print ("Record found for asset %s at date %s" % (price.asset, price.price_date))
			else:
				print ("EOF")
				session.add_all(prices)
				session.commit()
				break
		
class YahooProviderDataHandler:
	def extract_assets(responses):
		for response in responses:
			print (Asset(response.get("name"), response.get("symbol")))
			
	def extract_history_prices(asset, data):
		""" This method is expecting an Asset object and an array of data like this : [date, open, high, low, close] """
		if (data[0] == "Date"):
			return None
		else:
			reversedDate = data[0].split("-")
			reversedDate.reverse()
			return HistoryPrice(asset, 
				"/".join(reversedDate),
				data[1], data[2], data[3], data[4])
			
if __name__ == "__main__":
	#YahooProvider.lookup_ticker("yahoo")
	import profile
	profile.run('YahooProvider.history_extractor(db.session, "YHOO")');
	